package com.wilmer.yl.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wilmer.common.annotation.Anonymous;
import com.wilmer.vo.LineVo;
import org.apache.commons.compress.utils.Lists;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wilmer.common.annotation.Log;
import com.wilmer.common.core.controller.BaseController;
import com.wilmer.common.core.domain.AjaxResult;
import com.wilmer.common.enums.BusinessType;
import com.wilmer.yl.domain.ChildYl;
import com.wilmer.yl.service.IChildYlService;
import com.wilmer.common.utils.poi.ExcelUtil;
import com.wilmer.common.core.page.TableDataInfo;

/**
 * 孩子压力Controller
 * 
 * @author ruoyi
 * @date 2024-04-27
 */
@RestController
@RequestMapping("/yl/yl")
public class ChildYlController extends BaseController
{
    @Autowired
    private IChildYlService childYlService;

    /**
     * 查询孩子压力列表
     */
    @PreAuthorize("@ss.hasPermi('yl:yl:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChildYl childYl)
    {
        startPage();
        List<ChildYl> list = childYlService.selectChildYlList(childYl);
        return getDataTable(list);
    }

    /**
     * 导出孩子压力列表
     */
    @PreAuthorize("@ss.hasPermi('yl:yl:export')")
    @Log(title = "孩子压力", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChildYl childYl)
    {
        List<ChildYl> list = childYlService.selectChildYlList(childYl);
        ExcelUtil<ChildYl> util = new ExcelUtil<ChildYl>(ChildYl.class);
        util.exportExcel(response, list, "孩子压力数据");
    }

    /**
     * 获取孩子压力详细信息
     */
    @PreAuthorize("@ss.hasPermi('yl:yl:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(childYlService.selectChildYlById(id));
    }

    /**
     * 新增孩子压力
     */
    @PreAuthorize("@ss.hasPermi('yl:yl:add')")
    @Log(title = "孩子压力", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChildYl childYl)
    {
        return toAjax(childYlService.insertChildYl(childYl));
    }

    /**
     * 修改孩子压力
     */
    @PreAuthorize("@ss.hasPermi('yl:yl:edit')")
    @Log(title = "孩子压力", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChildYl childYl)
    {
        return toAjax(childYlService.updateChildYl(childYl));
    }

    /**
     * 删除孩子压力
     */
    @PreAuthorize("@ss.hasPermi('yl:yl:remove')")
    @Log(title = "孩子压力", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(childYlService.deleteChildYlByIds(ids));
    }

    /**
     * 查询孩子的压力值
     * @param userId
     * @param days
     * @return
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('yl:yl:daily')")
    @GetMapping("/yl")
    public LineVo ylChartsDaily(@Param("userId") Long userId, @Param("days") Long days){
        //获取压力值集合
        List<ChildYl> ylList = childYlService.ylChartsDaily(userId, days);

        //初始化Vo对象
        LineVo lineVo = new LineVo();
        lineVo.setXdata(Lists.newArrayList());
        lineVo.setSeries(Lists.newArrayList());

        ylList.forEach(yl ->{
            lineVo.getXdata().add(formatDate(yl.getDatetime()));
            lineVo.getSeries().add(yl.getYl());
        });
        return lineVo;
    }

    /**
     * 根据父母id查询孩子的压力列表
     * @param userId
     * @return
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('yl:yl:child')")
    @GetMapping("/child")
    public TableDataInfo getListByUserId(@Param("userId") Long userId){
        startPage();
        List<ChildYl> list = childYlService.getListByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 日期转换
     * @param date
     * @return
     */
    private String formatDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        return strDate;
    }
}
